import axios from "axios";
import { Toast } from "vant";
import { Notify } from "vant";
// import { uesRouter } from "vue-router";
// const router = uesRouter();
const http = axios.create({
  baseURL: "https://www.lexuemiao.com/api",
  timeout: 50000,
});

http.interceptors.request.use(
  (config) => {
    // console.log("config::::::::::", config);
    const token = localStorage.getItem("token");
    config.headers["zywxtoken"] = "zywx" + token;

    // if (config.url != "login") {
    //   const token = sessionStorage.getItem("token");
    // }
    Toast.loading({
      message: "加载中...",
      forbidClick: true,
      loadingType: "spinner",
    });
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

http.interceptors.response.use(
  (res) => {
    const { code, msg } = res.data;
    // console.log(res);
    const Arr = [200];
    if (Arr.includes(code)) {
      // Notify({ type: "success", message: msg });
      return res.data.data;
    }
    if (code !== 200) {
      Notify({ type: "danger", message: msg });
    }
    // if (!token) {
    //   router.push("/login");
    // }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default http;
